package util.common;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Collection处理辅助类.
 */
public class CollectionHelper {

    /**
     * 转换List
     * @param sources 源List
     * @param transformer 目标List
     * @param <T> 源类型
     * @param <S> 目标类型
     * @return 转换后的List
     */
    public static <T, S> List<T> convertList(List<S> sources, Transformer<S, T> transformer) {
        if (sources == null) {
            return null;
        }

        List<T> targets = new ArrayList<>();
        for (S source : sources) {
            T transform = transformer.transform(source);
            if (transform != null) {
                targets.add(transform);
            }
        }

        return targets;
    }

    public static <T, S> Set<T> convertSet(Set<S> sources, Transformer<S, T> transformer) {
        if (sources == null) {
            return null;
        }

        Set<T> targets = new HashSet<>();
        for (S source : sources) {
            targets.add(transformer.transform(source));
        }

        return targets;
    }

    public static List<String> convertStringToListUseSplit(String source, String split) {
        List<String> targets = new ArrayList<>();
        addSplitSringToCollection(source, split, targets);

        return targets;
    }

    private static void addSplitSringToCollection(String source, String split, Collection<String> targets) {
        if (StringUtils.isNotBlank(source)) {
            String[] strings = StringUtils.split(source, split);
            for (String s : strings) {
                if (StringUtils.isNotBlank(s)) {
                    String trimedString = StringUtils.trimToNull(s);
                    if (trimedString != null) {
                        targets.add(trimedString);
                    }
                }
            }
        }
    }

    public static Set<String> convertStringToSetUseSplit(String source, String split) {
        Set<String> targets = new LinkedHashSet<>(); //有顺序
        addSplitSringToCollection(source, split, targets);

        return targets;
    }

    /**
     * 按输入列表重复次数排序返回去重后的列表.
     */
    public static <T> List<T> uniqSort(List<T> inputList) {
        Map<T, Integer> map = new HashMap<>();

        // 遍历数组，将T及出现次数存放map中
        for (T str : inputList) {
            Integer count = map.get(str);
            if (count != null) {
                map.put(str, count + 1);
            } else {
                map.put(str, 1);
            }
        }

        // 定义list，存放map中的entry
        List<Map.Entry<T, Integer>> list = new ArrayList<>();
        list.addAll(map.entrySet());

        // 对list中的entry，按照value值进行降序排列
        Collections.sort(list, new Comparator<Map.Entry<T, Integer>>() {
            public int compare(Map.Entry<T, Integer> arg0, Map.Entry<T, Integer> arg1) {
                return arg1.getValue().compareTo(arg0.getValue());
            }
        });

        // 存放返回的列表
        List<T> retStr = new ArrayList<>();
        for (Map.Entry<T, Integer> entry : list) {
            retStr.add(entry.getKey());
        }

        return retStr;
    }
}
